package com.woniuxy.yoga.admin.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.admin.dao.model.Menu;
import com.woniuxy.yoga.admin.model.dto.menu.MenuDTO;
import com.woniuxy.yoga.admin.model.form.menu.*;
import com.woniuxy.yoga.admin.model.param.menu.*;
import com.woniuxy.yoga.admin.permission.Permission;
import com.woniuxy.yoga.admin.service.MenuService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.BusinessState;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年12月11日
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单相关接口")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增一级菜单
     * @param form
     * @return
     */
    @PostMapping("/addFirstMenu.api")
    @ApiOperation("新增一级菜单")
    @YogaLog("新增一级菜单")
    @Permission(resources = {"/admin/menu/addFirstMenu.api"})
    public Result<String> addFirstMenu(@Valid @RequestBody AddFirstMenuForm form){
        AddFirstMenuParam param = BeanUtil.toBean(form, AddFirstMenuParam.class);
        menuService.addFirstMenu(param);
        return Result.getResult(String.class).setMessage("新增一级菜单成功");
    }

    /**
     * 查询所有一级菜单
     * @return
     */
    @GetMapping("/selectFirstMenu.api")
    @ApiOperation("查询所有一级菜单")
    @YogaLog("查询所有一级菜单")
    @Permission(resources = {"/admin/menu/selectFirstMenu.api"})
    public Result<List<Menu>> selectFirstMenu(){
        List<Menu> firstMenus = menuService.selectFirstMenu();
        return Result.getListResult(Menu.class).setMessage("查询所有一级菜单成功").setData(firstMenus);
    }

    /**
     * 新增二级菜单
     * @param form
     * @return
     */
    @PostMapping("/addSeconMenu.api")
    @ApiOperation("新增二级菜单")
    @YogaLog("新增二级菜单")
    @Permission(resources = {"/admin/menu/addSeconMenu.api"})
    public Result<String> addSeconMenu(@Valid @RequestBody AddSeconMenuForm form){
        AddSeconMenuParam param = BeanUtil.toBean(form, AddSeconMenuParam.class);
        menuService.addSeconMenu(param);
        return Result.getResult(String.class).setMessage("新增二级菜单成功");
    }

    /**
     * 管理员传入角色ID，分页查询该角色的所有一级权限
     * @param form
     * @return
     */
    @PostMapping("/selectFirstByRoleId.api")
    @ApiOperation("查询角色一级菜单权限")
    @YogaLog("查询角色一级菜单权限")
    @Permission(resources = {"/admin/menu/selectFirstByRoleId.api"})
    public Result<PageInfo> selectFirstByRoleId(@Valid @RequestBody SelectFirstByRoleIdForm form){
        System.out.println(BusinessState.BUSINESS_ACCOUNT_STATE_NORMAL);
        SelectFirstByRoleIdParam param = BeanUtil.toBean(form, SelectFirstByRoleIdParam.class);
        PageInfo pageInfo = menuService.selectFirstByRoleId(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询一级菜单权限成功").setData(pageInfo);
    }

    /**
     * 管理员传入角色ID，分页查询该角色的所有二级权限
     * @param form
     * @return
     */
    @PostMapping("/selectSeconByRoleId.api")
    @ApiOperation("查询角色二级菜单权限")
    @YogaLog("查询角色二级菜单权限")
    @Permission(resources = {"/admin/menu/selectSeconByRoleId.api"})
    public Result<PageInfo> selectSeconByRoleId(@Valid@RequestBody SelectSeconByRoleIdForm form){
        SelectSeconByRoleIdParam param = BeanUtil.toBean(form, SelectSeconByRoleIdParam.class);
        PageInfo pageInfo = menuService.selectSeconByRoleId(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询二级菜单权限成功").setData(pageInfo);
    }

    /**
     * 管理员传入角色ID，菜单ID，取消关联
     * @param form
     * @return
     */
    @PostMapping("/cancelRelation.api")
    @ApiOperation("取消菜单关联")
    @YogaLog("取消菜单关联")
    @Permission(resources = {"/admin/menu/cancelRelation.api"})
    public Result<String> cancelRelation(@Valid@RequestBody CancelRelationForm form){
        CancelRelationParam param = BeanUtil.toBean(form, CancelRelationParam.class);
        menuService.cancelRelation(param);
        return Result.getResult(String.class).setMessage("取消关联成功");
    }

    /**
     * 管理员传入角色ID，菜单ID，恢复关联
     * @param form
     * @return
     */
    @PostMapping("/renewRelation.api")
    @ApiOperation("关联菜单操作")
    @YogaLog("关联菜单操作")
    @Permission(resources = {"/admin/menu/renewRelation.api"})
    public Result<String> renewRelation(@Valid@RequestBody RenewRelationForm form){
        RenewRelationParam param = BeanUtil.toBean(form, RenewRelationParam.class);
        menuService.renewRelation(param);
        return Result.getResult(String.class).setMessage("关联菜单成功");
    }

    /**
     * 传入账号ID，查询该账号角色所拥有的菜单权限
     * @param authen_token
     * @return
     */
    @GetMapping("/selectMenuByAccountId.api")
    @ApiOperation("查询菜单权限操作")
    @YogaLog("查询菜单权限操作")
    public Result<List<MenuDTO>> selectMenuByAccountId(@RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        List<MenuDTO> menuDTOS = menuService.selectMenuByAccountId(accountId);
        return Result.getListResult(MenuDTO.class).setData(menuDTOS).setMessage("查询管理员菜单成功");
    }

}

